1579D - Productive Meeting - CodeForces Solution


constructive algorithms graphs greedy *1400

Please click on ads to support us..

Python Code:

from heapq import heappush, heappop, heapify
def getIntegerInputs():
    return map(int, input().split())

def getListInput():
    return list(map(int, input().split()))
    
def inbound(r, c, n, m):
    return 0 <= r < n and 0 <= c < m
    
def solve():
    for _ in range(int(input())):
        ans = []
        n = int(input())
        arr = getListInput()
        heap = [(-val, i) for i, val in enumerate(arr)]
        heapify(heap)
        while len(heap) > 1:
            r = heappop(heap)
            l = heappop(heap)
            if r[0] == 0 or l[0] == 0:
                break
            
            heappush(heap, (l[0] + 1, l[1]))
            heappush(heap, (r[0] + 1, r[1]))
                
            ans.append((r[1]+1, l[1]+1))
                
        print(len(ans))
        for a in ans:
            print(*a)
if __name__ == "__main__":
    solve()

C++ Code:

// Author- Aryan Pundir
#include<bits/stdc++.h>
using namespace std;
#define ll          long long
#define lli         long long int
#define vl          vector<ll>
#define vi          vector<int>
#define vs          vector<string>
#define vpi          vector<pair<int,int>>
#define vpl          vector<pair<ll,ll>>
#define pi          pair<int,int>
#define pl          pair<ll,ll>
#define all(a)      a.begin(),a.end()
#define mem(a,x)    memset(a,x,sizeof(a))
#define pb          push_back
#define mpi          map<int,int>
#define mpl          map<ll,ll>
#define pqi          priority_queue<int>
#define pql          priority_queue<ll>
#define F           first
#define S           second
#define f(a,b) for(int i=a;i<b;i++)
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vi nums(n);
        vpi ans;
        priority_queue<pi> pqp;
        for(int i=0;i<n;i++){
            cin>>nums[i];
            if(nums[i]){pqp.push({nums[i],i+1});}
        }
        while(pqp.size()>1){
            pi f=pqp.top();
            pqp.pop();
            pi s=pqp.top();
            pqp.pop();
            ans.push_back({f.second,s.second});
            if(f.first>1){pqp.push({f.first-1,f.second});}
            if(s.first>1){pqp.push({s.first-1,s.second});}
        }
        cout<<ans.size()<<endl;
        for(auto &x:ans){
            if(x.first>x.second) swap(x.first,x.second);
            cout<<x.first<<" "<<x.second<<endl;
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even